package io.framework.modules.pollutionsource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.pollutionsource.dao.TBasPollutionlabeldirDao;
import io.framework.modules.pollutionsource.dto.PollutionlabeldirGoordInfoDTO;
import io.framework.modules.pollutionsource.dto.TBasPollutionlabeldirDTO;
import io.framework.modules.pollutionsource.entity.TBasPollutionlabeldirEntity;
import io.framework.modules.pollutionsource.service.TBasPollutionlabeldirService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 基础-污染源标签年份表名录（重点排污单位，环统单位）
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-24
 */
@Service
public class TBasPollutionlabeldirServiceImpl extends CrudServiceImpl<TBasPollutionlabeldirDao, TBasPollutionlabeldirEntity, TBasPollutionlabeldirDTO> implements TBasPollutionlabeldirService {

    @Override
    public QueryWrapper<TBasPollutionlabeldirEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<TBasPollutionlabeldirEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public PageData<TBasPollutionlabeldirDTO> pages(Map<String , Object> params){

        IPage<TBasPollutionlabeldirEntity> pages = getPage(params, Constant.CREATE_TIME, false);

        List<TBasPollutionlabeldirEntity> list = baseDao.getList(params);

        return getPageData(list,pages.getTotal(),TBasPollutionlabeldirDTO.class);
    }

    public Map<String, Object> getParamsMap(Map<String, Object> params){
        String regionName[],attentionDegreeName[];
        if(params.containsKey("regionName")){
            String name= params.get("regionName").toString();
            if(StringUtils.isNotBlank(name)){
                regionName = name.split(",");
                params.put("regionName",regionName);
            }
        }
        if(params.containsKey("attentionDegreeName")){
            String name= params.get("attentionDegreeName").toString();
            if(StringUtils.isNotBlank(name)){
                attentionDegreeName = name.split(",");
                params.put("attentionDegreeName",attentionDegreeName);
            }
        }
        return params;
    }

    @Override
    public List<TBasPollutionlabeldirDTO> lists(Map<String , Object> params){

        params = getParamsMap(params);

        List<TBasPollutionlabeldirEntity> list =  baseDao.getList(params);
        return ConvertUtils.sourceToTarget(list,TBasPollutionlabeldirDTO.class);
    }

    @Override
    public List<PollutionlabeldirGoordInfoDTO> getCoordList(Map<String, Object> params){

        params = getParamsMap(params);

        List<TBasPollutionlabeldirEntity> list =  baseDao.getCoordList(params);
        return ConvertUtils.sourceToTarget(list,PollutionlabeldirGoordInfoDTO.class);
    }

}